//
//  Status.swift
//  SinaWeibo
//
//  Created by yang on 15/11/14.
//  Copyright © 2015年 yang. All rights reserved.
//

import UIKit

class Status: NSObject {
    //微博创建时间
   var created_at:String?
    //微博ID
   var id:Int = 0
    //微博信息内容
   var text:String?
    //微博来源
   var source:String?
    //微博作者的用户信息字段
    var user:User?
    //配图地址数组
    var pic_urls:[[String:String]]?
    //获得配图的URL
    var urlImages:[NSURL]?{
        guard let urls = pic_urls else{
            return nil
        }
        var array = [NSURL]()
        for item in urls{
            let url = NSURL(string: item["thumbnail_pic"]!)
            array.append(url!)
        }
        return array
    }
    //单张图片时返回的 URL
    var url:[NSURL]?{
        if retweeted_status == nil {
            //原创微博
        return urlImages
        }
        //转发微博
        return retweeted_status?.urlImages
    }
    //被转发微博
    var  retweeted_status: Status?
    //微博的来源
    var weiboSource: String? {
        if retweeted_status != nil {
            //被转发微博的来源
            return retweeted_status?.source
        }
        //原创微博的来源
        return source
    }
    init(dict:[String:AnyObject]) {
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String:AnyObject]{
                user = User(dict:dict)
            }
        return
        }
        if key == "retweeted_status"{
            if let dict = value as? [String : AnyObject]{
            retweeted_status = Status(dict: dict)
            }
         return
        }
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
}
